package com.tool.configuration.interceptor;

import com.tool.configuration.jwt.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 酒徒
 * @Version 1.0
 * @data 2024/1/25 13:24
 * 拦截器jwt登录校验
 */
@Component
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.判断当前拦截到的是controller的方法还是其他资源
        if (!(handler instanceof HandlerMethod)) {
            //不是动态方法，放行
            return true;
        }

        //2.获取token令牌
        String token = request.getHeader("token");
        //3.校验令牌
        try {
            Claims claims = JwtUtil.parseJwt("jwt", token);
        } catch (Exception e) {
            //不通过
            return false;
        }
        //4.通过，放行
        return true;
    }
}
